package com.yingxin.yx.framework.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.yingxin.yx.framework.user.core.IUserSupport;
import com.yingxin.yx.framework.user.dto.UserDto;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author fanchao
 */
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {

    @Autowired
    private IUserSupport userSupport;

    @Override
    public void insertFill(MetaObject metaObject) {
        UserDto user = userSupport.getUser();
        if (user != null) {
            // obj
            /*
            Object obj;
            if (metaObject.hasGetter("et")) {
                obj = metaObject.getValue("et");
            } else {
                obj = metaObject.getOriginalObject();
            }
            Class<?> clz = obj.getClass();
            Field[] fields = clz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if("creatorId".equals(field.getName())){
                    userId(obj, field, user);
                }

                if("updaterId".equals(field.getName())){
                    userId(obj, field, user);
                }
            }
            */
            if (user.getUserId() != null) {
                this.setFieldValByName("creatorId", user.getUserId(), metaObject);
                this.setFieldValByName("updaterId", user.getUserId(), metaObject);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        UserDto user = userSupport.getUser();
        if (user != null) {
            // obj
            /*
            Object obj;
            if (metaObject.hasGetter("et")) {
                obj = metaObject.getValue("et");
            } else {
                obj = metaObject.getOriginalObject();
            }
            Class<?> clz = obj.getClass();
            Field[] fields = clz.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);
                if("updaterId".equals(field.getName())){
                    userId(obj, field, user);
                }
            }
            */
            if (user.getUserId() != null) {
                this.setFieldValByName("updaterId", user.getUserId(), metaObject);
            }
        }
        this.setFieldValByName("updateTime", new Date(), metaObject);
    }


    private void userId(Object obj, Field field, UserDto user) {
        try {
            String fieldVal = (String) field.get(obj);
            if (fieldVal == null) {
                fieldVal = user.getUserId();
                field.set(obj, fieldVal);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }


        /*
    private void organizationId(Object obj, Field field, UserDto user) {
        try {
            String fieldName = field.getName();
            String fieldVal = (String) field.get(obj);
            if (fieldVal == null) {
                Map<String, String> organizationIds = user.getOrganizationIds();
                fieldVal = organizationIds.get(fieldName);
                // fieldVal = user.getOrganizationId();
                field.set(obj, fieldVal);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    */


                /*
                CreatorId creatorIdAnnotation = field.getAnnotation(CreatorId.class);
                if (creatorIdAnnotation != null) {
                    userId(obj, field, user);
                }

                UpdaterId updaterIdAnnotation = field.getAnnotation(UpdaterId.class);
                if (updaterIdAnnotation != null) {
                    userId(obj, field, user);
                }
                */
}